package org.lucky.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lucky.admin.query.LuckyShareQuery;
import org.lucky.admin.query.UserQuery;
import org.lucky.admin.service.IUserService;
import org.lucky.admin.util.ExcelUtil;
import org.lucky_common.domain.Admin;
import org.lucky_common.domain.LuckyShare;
import org.lucky_common.domain.User;
import org.lucky_common.util.AjaxResult;
import org.lucky_common.util.PageList;
import org.lucky_common.vo.LuckyShareExportVo;
import org.lucky_common.vo.UserExportVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class AdminUserController {

    @Resource
    private IUserService userService;

    @PutMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody User user) {
        boolean b = userService.saveOrUpdate(user);
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("Error saving employee");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        boolean b = userService.removeById(id);
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("Error");
        }
    }

    @DeleteMapping("/deleteList")
    public AjaxResult deleteList(@RequestBody ArrayList<Long> ids) {
        boolean b = userService.removeByIds(ids);
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error("Error");
        }
    }

    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        return AjaxResult.success(userService.getById(id));
    }

    @PostMapping
    public AjaxResult queryPage(@RequestBody UserQuery userQuery) {
        Page<User> page = new Page<User>(userQuery.getPage(),userQuery.getRows(),true);
        if (userQuery.getStart().equals("") && userQuery.getEnd().equals("")){
            page = userService.page(page,new QueryWrapper<User>().like("uname",userQuery.getKeyword()).or().like("email",userQuery.getKeyword()).orderByDesc("data"));
            PageList<User> pageList = new PageList<User>(page.getTotal(),page.getRecords());
            return AjaxResult.success(pageList);
        }else {
            page = userService.page(page,new QueryWrapper<User>().like("uname",userQuery.getKeyword()).between("data",userQuery.getStart(),userQuery.getEnd()));
            PageList<User> pageList = new PageList<User>(page.getTotal(),page.getRecords());
            return AjaxResult.success(pageList);
        }
    }

    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        //查询要导出的数据
        List<UserExportVo> users = userService.getUserExportList();
        UserExportVo.threadLocal_collectionSize.set( users.size() );
        UserExportVo.threadLocal_insertIndex.set( 1 );
        ExcelUtil.exportExcelX(users, "用户信息表", "sheet1", UserExportVo.class, "用户信息表.xlsx", response);
    }

    @PutMapping("/exportMore")
    public void exportMore11(@RequestBody ArrayList<UserExportVo> data,HttpServletResponse response) {
        List<UserExportVo> userExportVo = new ArrayList<>(data);
        UserExportVo.threadLocal_collectionSize.set( userExportVo.size() );
        UserExportVo.threadLocal_insertIndex.set( 1 );
        ExcelUtil.exportExcelX(userExportVo, "用户信息表", "sheet1", UserExportVo.class, "用户信息表.xlsx", response);
    }
}
